<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class User extends Controller
{
    //用户注册
    public function Reg(Request $request)
    {
        $p = $request->post();
    //处理数据
        $data = [
            'username' => $p['user'],
            'pass' => md5($p['passwd']),
            'phone' => $p['iphone'],
        ];
    // 判断用户名是否存在
        $m = Db::table('user')->field('count(id)')->where('username', $p['user'])->find();
        if (!empty($m['count(id)'])) {

            $Data[0] =
            $this->error('用户名已存在,请重新注册!');

        } else{
            $result = Db::name('user')->data($data)->insert();
            $this->success('注册成功，请登录', 'index/index/login');
        }

    }
    //用户登录
    public function index()
    {
    // 如果已经登录,则跳转首页
        if (Session::get('user', 'index')) {
            $this->redirect('index/index/index');
        }
        return view('index/login');
    }
    //用户登陆 __处理
    public function login()
    {
        $data=[];
        $status='ok';
        $info='';
        if (!empty($_POST['u'])&&!empty($_POST['p'])){
            $data['username']=$_POST['u'];
            $data['pass']=md5($_POST['p']);
            $m=Db::table('user')->where($data)->find();
            if ($m){
                $status='ok';
                $info='登陆成功';
                $this->login($data['username']);
            }else{
                $status='error';
                $info='用户名或密码不正确';
            }
        }else{
            $status='error';
            $info='用户名或密码不能为空';
        }
        echo json_encode(['status'=>$status,'info'=>$info]);
    }

    //修改个人信息
    public function update(Request $request)
    {
        $info = $request->put();

        $data = [
            'username' => $info['username'],
            'phone' => $info['iphone'],
        ];
//        dump($data);die;

        //执行更新
        $result = Db::name('user')->where('id',$id)->update($data);
        //判断执行情况
        if ($result > 0) {
            return $this->success('编辑成功', url('index/PersonalCenter'));
        } else {
            return $this->success('编辑失败(ノ°ο°)ノ高能预警!');
        }
    }

//    //收藏页面
//    public function liked()
//    {
//        if($userInfo=Session::get('user')){
//            $likeWhere=['userid'=>$userInfo['userId']];
//            $imgData=[];
//            $fileId=[];
//            if($likedData=Db::table('shops_liked')->order('id desc')->where($likeWhere)->select()){
//                foreach($likedData as $k=>$vo){
//                    $fileId[]=$vo;
//                    $imgData[$k]=Db::table('')->where('id',$vo['imgid'])->find();
//                    $imgData[$k]['likedId']=$vo['id'];
//                };
//                $this->assign('likedCount',count($fileId));
//            }
//            $this->assign('imgData',$imgData);
//            return $this->fetch();
//        }else{
//            header("Location:/index/login");exit;
//        }
//    }
}
